package com.jincp.filepicker.ui.video;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;

import com.jincp.common.permission.MPermission;
import com.jincp.common.utils.AppUtils;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.Constant;
import com.jincp.filepicker.R;
import com.jincp.filepicker.bean.BaseFile;
import com.jincp.filepicker.bean.VideoFile;
import com.jincp.filepicker.base.BasePickerManager;
import com.jincp.filepicker.util.MediaFileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: VideoPickerManager
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/23 23:44
 * =========================================================
 */
public class VideoPickerManager extends BasePickerManager<VideoPickerManager>{

    public static final String TAG = VideoPickerManager.class.getSimpleName();

    public static final int REQUEST_VIDEO_TAKE = 3001;
    public static final int REQUEST_VIDEO_PREVIEW = 3003;

    public static final int RESULT_VIDEO_ITEMS = 3004;
    public static final int RESULT_VIDEO_ITEM = 3005;

    private boolean isShowCamera = true;   //显示相机
    private File takeVideoFile;  //拍摄保存视频文件

    private static volatile VideoPickerManager mInstance;

    private VideoPickerManager() {
    }

    public static VideoPickerManager getInstance() {
        if (mInstance == null) {
            synchronized (VideoPickerManager.class) {
                if (mInstance == null) {
                    mInstance = new VideoPickerManager();
                }
            }
        }
        return mInstance;
    }

    public VideoPickerManager reset() {
        setMultiMode(false);
        setMaxSelectedLimit(9);
        setShowCamera(false);
        return this;
    }

    public VideoPickerManager setShowCamera(boolean showCamera) {
        isShowCamera = showCamera;
        return this;
    }

    public boolean isShowCamera() {
        return isShowCamera;
    }

    public File getTakeVideoFile() {
        return takeVideoFile;
    }

    /**
     * 拍照的方法
     */
    public void takeVideo(Activity activity) {
        if (activity == null || activity.isFinishing()) return;
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(activity, Constant.PERMISSION)) {
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            takeVideoIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            if (createFile("VID_", ".mp4")) {
                Uri uri = AppUtils.getFileUri(activity, takeVideoFile);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    takeVideoIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                } else {
                    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                }
                takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            } else {
                ToastUtil.getInstance().show(R.string.create_file_failure);
                return;
            }
            if (MediaFileUtil.detectIntent(activity, takeVideoIntent)) {
                activity.startActivityForResult(takeVideoIntent, VideoPickerManager.REQUEST_VIDEO_TAKE);
            } else {
                ToastUtil.getInstance().show(R.string.no_video_app);
            }
        } else {
            MPermission.with(activity).permissions(Constant.PERMISSION).request();
        }
    }

    /**
     * 拍照的方法
     */
    public void takeVideo(Fragment fragment) {
        if (fragment == null) return;
        Activity activity = fragment.getActivity();
        if (activity == null || activity.isFinishing()) return;
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(activity, Constant.PERMISSION)) {
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            takeVideoIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            if (createFile("VID_", ".mp4")) {
                Uri uri = AppUtils.getFileUri(activity, takeVideoFile);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    takeVideoIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                } else {
                    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                }
                takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            } else {
                ToastUtil.getInstance().show(R.string.create_file_failure);
                return;
            }
            if (MediaFileUtil.detectIntent(fragment.getActivity(), takeVideoIntent)) {
                fragment.startActivityForResult(takeVideoIntent, VideoPickerManager.REQUEST_VIDEO_TAKE);
            } else {
                ToastUtil.getInstance().show(R.string.no_video_app);
            }
        } else {
            MPermission.with(fragment).permissions(Constant.PERMISSION).request();
        }
    }

    /**
     * 选择照片的方法
     */
    public void pickVideo(Activity activity) {
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(activity, Constant.PERMISSION)) {
            Intent pickVideoIntent = new Intent(activity, VideoPickActivity.class);
            activity.startActivityForResult(pickVideoIntent, VideoPickerManager.REQUEST_VIDEO_TAKE);
        } else {
            MPermission.with(activity).permissions(Constant.PERMISSION).request();
        }
    }

    /**
     * 选择照片的方法
     */
    public void pickVideo(Fragment fragment) {
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(fragment.getActivity(), Constant.PERMISSION)) {
            Intent pickVideoIntent = new Intent(fragment.getActivity(), VideoPickActivity.class);
            fragment.startActivityForResult(pickVideoIntent, VideoPickerManager.REQUEST_VIDEO_TAKE);
        } else {
            MPermission.with(fragment).permissions(Constant.PERMISSION).request();
        }
    }

    /**
     * 产生一个图片文件
     */
    public boolean createFile(String prefix, String suffix) {
        try {
            takeVideoFile = MediaFileUtil.createFile("video", prefix, suffix);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void previewVideo(Activity activity, VideoFile videoFile) {
        File file = new File(videoFile.getPath());
        Intent previewVideoIntent = new Intent(Intent.ACTION_VIEW);
        Uri uri = AppUtils.getFileUri(activity, file);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            previewVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            previewVideoIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        previewVideoIntent.setDataAndType(uri, "video/mp4");
        if (MediaFileUtil.detectIntent(activity, previewVideoIntent)) {
            activity.startActivity(previewVideoIntent);
        } else {
            ToastUtil.getInstance().show(R.string.no_video_play_app);
        }
    }

    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == VideoPickerManager.REQUEST_VIDEO_TAKE){
            VideoFile videoFile = new VideoFile();
            videoFile.setPath(takeVideoFile.getAbsolutePath());
            clear();
            addSelectedFile(videoFile);
            completedSelector();
            MediaFileUtil.notifyMediaChanged(activity, takeVideoFile);
        }
    }
}